using CollabApp.Domain.Entities.Game;

namespace CollabApp.Domain.Services.Game;

/// <summary>
/// 圈地游戏道具系统服务接口
/// 负责管理游戏中的四种核心道具：闪电、护盾、炸弹、幽灵
/// 实现智能刷新机制、道具效果管理和平衡性控制
/// </summary>
public interface IPowerUpService
{
    /// <summary>
    /// 智能生成道具
    /// 根据玩家密度和领地分布调整刷新位置，优先在无人领地区域生成
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="excludeOccupiedAreas">是否排除被占领的区域</param>
    /// <returns>生成的道具列表</returns>
    Task<List<PowerUpInstance>> SpawnPowerUpsAsync(Guid gameId, bool excludeOccupiedAreas = true);

    /// <summary>
    /// 玩家拾取道具
    /// 玩家接近道具时自动拾取，每个玩家最多持有1个道具
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <param name="powerUpId">道具标识</param>
    /// <param name="playerPosition">玩家位置</param>
    /// <returns>拾取结果</returns>
    Task<PowerUpPickupResult> PickupPowerUpAsync(Guid gameId, Guid playerId, Guid powerUpId, Position playerPosition);

    /// <summary>
    /// 使用闪电道具
    /// 效果：移动速度提升60%，持续8秒，但画线轨迹更粗（3像素）更易被发现
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>闪电道具使用结果</returns>
    Task<LightningUseResult> UseLightningPowerUpAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 使用护盾道具
    /// 效果：免疫一次截断攻击，持续12秒或触发一次保护，使用时移动速度降低10%
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>护盾道具使用结果</returns>
    Task<ShieldUseResult> UseShieldPowerUpAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 使用炸弹道具
    /// 效果：以当前位置为中心，半径30像素范围变成领地，只能在中立区域或己方领地使用
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <param name="targetPosition">目标位置</param>
    /// <returns>炸弹道具使用结果</returns>
    Task<BombUseResult> UseBombPowerUpAsync(Guid gameId, Guid playerId, Position targetPosition);

    /// <summary>
    /// 使用幽灵道具
    /// 效果：10秒内可以穿越敌方轨迹而不死亡，但不能圈地
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>幽灵道具使用结果</returns>
    Task<GhostUseResult> UseGhostPowerUpAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 获取玩家当前道具
    /// 每个玩家最多持有1个道具
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>玩家持有的道具，如果没有返回null</returns>
    Task<PlayerPowerUp?> GetPlayerPowerUpAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 获取玩家活跃道具效果
    /// 获取玩家当前所有活跃的道具效果（闪电、护盾、幽灵等）
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>活跃效果列表</returns>
    Task<List<ActivePowerUpEffect>> GetActiveEffectsAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 获取地图上所有道具
    /// 获取当前地图上存在的所有道具点
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>地图道具列表</returns>
    Task<List<PowerUpInstance>> GetMapPowerUpsAsync(Guid gameId);

    /// <summary>
    /// 更新道具效果状态
    /// 每帧调用，更新所有活跃道具效果的剩余时间和状态
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="deltaTime">时间增量（毫秒）</param>
    /// <returns>更新结果，包含过期的效果列表</returns>
    Task<PowerUpUpdateResult> UpdatePowerUpEffectsAsync(Guid gameId, long deltaTime);

    /// <summary>
    /// 检查护盾是否能阻挡攻击
    /// 当玩家轨迹被攻击时检查是否有护盾保护
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">被攻击的玩家标识</param>
    /// <returns>护盾检查结果</returns>
    Task<ShieldBlockResult> CheckShieldBlockAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 检查幽灵状态
    /// 检查玩家是否处于幽灵状态（可以穿越敌方轨迹）
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>是否处于幽灵状态</returns>
    Task<bool> IsPlayerInGhostModeAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 获取玩家当前移动速度
    /// 考虑闪电道具和护盾道具的速度影响
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="playerId">玩家标识</param>
    /// <returns>当前移动速度倍率</returns>
    Task<float> GetPlayerSpeedMultiplierAsync(Guid gameId, Guid playerId);

    /// <summary>
    /// 清理过期道具
    /// 清理地图上过期的道具，为新道具让出空间
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>清理的道具数量</returns>
    Task<int> CleanupExpiredPowerUpsAsync(Guid gameId);

    /// <summary>
    /// 获取道具刷新配置
    /// 根据游戏模式获取道具刷新间隔和数量配置
    /// </summary>
    /// <param name="gameMode">游戏模式</param>
    /// <returns>道具刷新配置</returns>
    PowerUpSpawnConfig GetPowerUpConfig(string gameMode);

    /// <summary>
    /// 获取游戏中活跃的道具
    /// 返回地图上可拾取的道具和正在生效的玩家道具效果
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>活跃道具列表</returns>
    Task<ActivePowerUpsResult> GetActivePowerUpsAsync(Guid gameId);
}

/// <summary>
/// 圈地游戏道具类型枚举
/// </summary>
public enum TerritoryGamePowerUpType
{
    /// <summary>
    /// 闪电道具（蓝色）：移动速度提升60%，持续8秒，但轨迹更粗
    /// </summary>
    Lightning,
    
    /// <summary>
    /// 护盾道具（金色）：免疫一次截断攻击，持续12秒或触发一次
    /// </summary>
    Shield,
    
    /// <summary>
    /// 炸弹道具（红色）：以当前位置为中心，半径30像素范围变成领地
    /// </summary>
    Bomb,
    
    /// <summary>
    /// 幽灵道具（紫色）：10秒内可以穿越敌方轨迹而不死亡，但不能圈地
    /// </summary>
    Ghost
}

/// <summary>
/// 道具实例
/// </summary>
public class PowerUpInstance
{
    public Guid Id { get; set; }
    public TerritoryGamePowerUpType Type { get; set; }
    public Position Position { get; set; } = new();
    public DateTime SpawnTime { get; set; }
    public bool IsActive { get; set; } = true;
    public string Color { get; set; } = string.Empty;
    public string Icon { get; set; } = string.Empty;
    public Dictionary<string, object> Properties { get; set; } = new();
}

/// <summary>
/// 道具拾取结果
/// </summary>
public class PowerUpPickupResult
{
    public bool Success { get; set; }
    public TerritoryGamePowerUpType PowerUpType { get; set; }
    public string? ReplacedPowerUp { get; set; }
    public List<string> Messages { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 闪电道具使用结果
/// </summary>
public class LightningUseResult
{
    public bool Success { get; set; }
    public float SpeedMultiplier { get; set; } = 1.6f; // 60%提升
    public int DurationSeconds { get; set; } = 8;
    public float TrailThickness { get; set; } = 3f; // 更粗的轨迹
    public DateTime EffectEndTime { get; set; }
    public List<string> Messages { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 护盾道具使用结果
/// </summary>
public class ShieldUseResult
{
    public bool Success { get; set; }
    public int DurationSeconds { get; set; } = 12;
    public float SpeedPenalty { get; set; } = 0.9f; // 10%速度降低
    public DateTime EffectEndTime { get; set; }
    public int BlocksRemaining { get; set; } = 1;
    public List<string> Messages { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 炸弹道具使用结果
/// </summary>
public class BombUseResult
{
    public bool Success { get; set; }
    public Position ExplosionCenter { get; set; } = new();
    public float ExplosionRadius { get; set; } = 30f;
    public decimal AreaGained { get; set; }
    public List<Position> NewTerritory { get; set; } = new();
    public List<Guid> AffectedPlayers { get; set; } = new();
    public List<string> Messages { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 幽灵道具使用结果
/// </summary>
public class GhostUseResult
{
    public bool Success { get; set; }
    public int DurationSeconds { get; set; } = 10;
    public DateTime EffectEndTime { get; set; }
    public bool CanDrawWhileGhost { get; set; } = false; // 不能圈地
    public List<string> Messages { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 玩家道具状态
/// </summary>
public class PlayerPowerUp
{
    public Guid PlayerId { get; set; }
    public TerritoryGamePowerUpType? PowerUpType { get; set; }
    public DateTime? ObtainedTime { get; set; }
    public bool CanUse { get; set; } = true;
}

/// <summary>
/// 活跃道具结果
/// </summary>
public class ActivePowerUpsResult
{
    public bool Success { get; set; }
    public List<PowerUpInstance> MapPowerUps { get; set; } = new();
    public List<PlayerPowerUpInfo> PlayerPowerUps { get; set; } = new();
    public List<ActivePowerUpEffect> ActiveEffects { get; set; } = new();
    public List<string> Errors { get; set; } = new();
}

/// <summary>
/// 玩家道具信息
/// </summary>
public class PlayerPowerUpInfo
{
    public Guid PlayerId { get; set; }
    public string PlayerName { get; set; } = string.Empty;
    public PlayerPowerUp? HeldPowerUp { get; set; }
    public List<ActivePowerUpEffect> ActiveEffects { get; set; } = new();
}

/// <summary>
/// 活跃道具效果
/// </summary>
public class ActivePowerUpEffect
{
    public Guid EffectId { get; set; }
    public Guid PlayerId { get; set; }
    public TerritoryGamePowerUpType Type { get; set; }
    public string Name { get; set; } = string.Empty;
    public DateTime StartTime { get; set; }
    public int DurationSeconds { get; set; }
    public DateTime EndTime { get; set; }
    public bool IsActive { get; set; } = true;
    public Dictionary<string, float> Effects { get; set; } = new();
}

/// <summary>
/// 道具更新结果
/// </summary>
public class PowerUpUpdateResult
{
    public int ActiveEffectsCount { get; set; }
    public int ExpiredEffectsCount { get; set; }
    public List<Guid> ExpiredEffectIds { get; set; } = new();
    public List<Guid> NewlyActivatedEffects { get; set; } = new();
}

/// <summary>
/// 护盾阻挡结果
/// </summary>
public class ShieldBlockResult
{
    public bool HasShield { get; set; }
    public bool BlockedAttack { get; set; }
    public bool ShieldExpired { get; set; }
    public int RemainingBlocks { get; set; }
    public DateTime? ShieldEndTime { get; set; }
}

/// <summary>
/// 道具刷新配置
/// </summary>
public class PowerUpSpawnConfig
{
    public int MaxConcurrentPowerUps { get; set; } = 3;
    public int SpawnIntervalSeconds { get; set; } = 25;
    public Dictionary<TerritoryGamePowerUpType, float> SpawnWeights { get; set; } = new()
    {
        { TerritoryGamePowerUpType.Lightning, 0.3f },
        { TerritoryGamePowerUpType.Shield, 0.3f },
        { TerritoryGamePowerUpType.Bomb, 0.2f },
        { TerritoryGamePowerUpType.Ghost, 0.2f }
    };
    public List<Position> PreferredSpawnAreas { get; set; } = new();
    public bool AvoidPlayerTerritories { get; set; } = true;
}
